package com.java.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.java.dao.ActivityRecordDao;
import com.java.dao.ActivitySlotDao;
import com.java.domain.ActivitySlot;
import com.java.domain.event.ActivityRecordEvent;
import com.java.domain.event.ActivitySlotEvent;
import com.java.service.SlotService;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class SlotServiceImpl implements SlotService {

	@Resource ActivitySlotDao slotDao;
	@Resource ActivityRecordDao recordDao;

	@Override
	public TableRespone getListBySport(String sportId, Integer enable) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("slotSportId", sportId);
		if(!MyTools.isEmpty(enable)) {
			params.put("slotEnable", enable);
		}
		List<ActivitySlotEvent> list = slotDao.selectAll(params, 0, 0);
		if(null != list) {
			/*for (ActivitySlotEvent slot : list) {
				List<ActivityRecordEvent> rlist = recordDao.selectAllBySlot(sportId, slot.getSlotId(), null);
				if(null != rlist) {
					slot.setHasSelect(rlist.size());
				}
			}*/
		}
		long total = list.size();
		return new TableRespone(list, total);
	}

	@Override
	public ActivitySlotEvent getByDate(String slotId, String date) {
		ActivitySlotEvent info = slotDao.selectByKey(slotId);
		if(null != info) {
			List<ActivityRecordEvent> rlist = recordDao.selectAllBySlot(info.getSlotSportId(), slotId, date);
			if(null != rlist) {
				info.setHasSelect(rlist.size());
			}
		}
		return info;
	}

	@Override
	public ActivitySlotEvent getOne(String slotId) {
		ActivitySlotEvent info = slotDao.selectByKey(slotId);
		return info;
	}

	@Override
	public List<ActivitySlot> createSlot(String sportId) {
		List<ActivitySlot> list = new ArrayList<ActivitySlot>();
		int startTime = 6;
		int endTime = 22;
		int order = 1;
		for (int i = startTime; i <= endTime; i++) {
			String hour = i + ":00";
			if (i < 10) {
				hour = "0" + i + ":00";
			}
			ActivitySlot slot = new ActivitySlot()
			.setSlotId(MyTools.getUUID())
			.setSlotSportId(sportId)
			.setSlotOrder(order)
			.setSlotEnable(0)
			.setSlotCapacity(0)
			.setSlotPrice(new BigDecimal(0))
			.setSlotTime(hour);
			order ++;
			list.add(slot);
		}
		return list;
	}

	@Override
	public BaseRespone initSlot(String sportId) {
		List<ActivitySlot> list = createSlot(sportId);
		int row = slotDao.insertBatch(list);
		return BaseRespone.result(row, "Success");
	}

	@Override
	public BaseRespone saveSlot(List<ActivitySlot> list) {
		int row = slotDao.insertBatch(list);
		return BaseRespone.result(row, "Success");
	}

	@Override
	public BaseRespone clearSlot(String sportId) {
		int row = slotDao.deleteBySport(sportId);
		return BaseRespone.result(row, "Success");
	}

	@Override
	public BaseRespone updateSlot(ActivitySlot slot) {
		ActivitySlotEvent info = slotDao.selectByKey(slot.getSlotId());
		info.setSlotEnable(slot.getSlotEnable());
		info.setSlotCapacity(slot.getSlotCapacity());
		info.setSlotPrice(slot.getSlotPrice());
		int row = slotDao.updateSelective(info);
		return BaseRespone.result(row, "Success");
	}

}
